<template>
  <div class="bill-container">
    <el-card class="bill-card">
      <div class="title">
        <span class="desc">我的账单</span>
        <img class="icon" src="@/assets/img/item-img/bill.png" alt="" />
      </div>
      <div class="body">
        <el-tabs v-model="activeName" @tab-click="tabChange">
          <!-- 账单区域 -->
          <el-tab-pane label="我的账单" name="myBill">
            <el-table :data="bill" border stripe>
              <el-table-column type="index"></el-table-column>
              <el-table-column prop="createTime" label="日期"> </el-table-column>
              <el-table-column prop="amount" label="金额"> </el-table-column>
              <el-table-column prop="channel" label="消费渠道"> </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 充值记录区域 -->
          <el-tab-pane label="充值记录" name="charge">
            <el-table :data="chargeBill" border stripe>
              <el-table-column type="index"></el-table-column>
              <el-table-column prop="createTime" label="日期"> </el-table-column>
              <el-table-column prop="studentName" label="充值人"> </el-table-column>
              <el-table-column prop="studentNum" label="充值人校号"> </el-table-column>
              <el-table-column prop="amount" label="充值金额"> </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>
  </div>
</template>

<script>
import commonServe from '../../api/service/common.serve'
export default {
  data() {
    return {
      id: window.sessionStorage.getItem('studentId'),
      bill: [], //账单
      chargeBill: [], //充值记录
      activeName: 'myBill' //激活的名字
    }
  },
  created() {
    this.getBill()
    // this.getChargeBill()
  },
  methods: {
    // 获取账单
    getBill() {
      commonServe
        .getBill(this.id)
        .then((res) => {
          // console.log(res)
          if (res.code === 200) {
            this.bill = res.data.list
          } else {
            this.$message.warning('获取账单失败')
          }
        })
        .catch((err) => {
          this.$message.warning(err.message || '获取账单失败')
        })
    },
    // 获取充值记录
    getChargeBill() {
      // console.log(123)
      commonServe
        .getChargeBill(this.id)
        .then((res) => {
          console.log(res, 123)
          if (res.code === 200) {
            this.chargeBill = res.data.list
          } else {
            this.$message.warning('获取充值记录失败')
          }
        })
        .catch((err) => {
          this.$message.warning(err.message || '获取充值记录失败')
        })
    },
    // tab页面更改
    tabChange() {
      console.log(this.activeName)
      if (this.activeName == 'charge') {
        this.getChargeBill()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.bill-container {
  .bill-card {
    .title {
      display: flex;
      align-items: center;
      .desc {
        font-size: 30px;
        color: #3a62d7;
        letter-spacing: 10px;
        font-weight: bold;
        text-shadow: 2px 2px 5px #999;
      }
      .icon {
        width: 40px;
        height: 40px;
      }
      .icon:hover {
        transform: translateY(-5px);
        transition: 0.5s;
      }
    }
  }
}
</style>
